'use strict'
const { isNotEmpty } = require('./util')

const tableName = {}
export const sqlObj = {}
export const getSql = {}

// 案件基本信息
sqlObj.ajjbxxObj = {
  bmsah: null,
  tysah: null,
  sfsc: null,
  sfygxtsj: null,
  cbdw_bm: null,
  cbdw_mc: null,
  fqdwbm: null,
  fql: null,
  cjsj: null,
  zhxgsj: null,
  slrq: null,
  ajmc: null,
  ajlb_bm: null,
  ajlb_mc: null,
  zcjg_dwdm: null,
  zcjg_dwmc: null,
  ysdw_dwdm: null,
  ysdw_dwmc: null,
  yswswh: null,
  ysay_aydm: null,
  ysay_aymc: null,
  ysqtay_aydms: null,
  ysqtay_aymcs: null,
  cbrgh: null,
  cbr: null,
  cbbm_bm: null,
  cbbm_mc: null,
  ajzt: null,
  sfswaj: null,
  sfdbaj: null,
  zxhd_mc: null,
  wcrq: null,
  gdrq: null,
  gdrgh: null,
  gdr: null,
  aqzy: null,
  dqjd: null,
  blksrq: null,
  blts: null,
  dqrq: null,
  bjrq: null,
  yjzt: null,
  jyyjzt: null,
  jyyjhcqxyrgs: null,
  lcslbh: null,
  fxdj: null,
  sfgzaj: null,
  fz: null,
  ysyj_dm: null,
  ysyj_mc: null,
  sfjbaj: null,
  zxhd_dm: null,
  dqyjjd: null,
  yascssjd_dm: null,
  yascssjd_mc: null,
  ysrjdh: null,
  sfzxdj: null
}
tableName.ajjbxxObjTableName = 'tyyw_gg_ajjbxx'

// 用户信息
sqlObj.userObj = { unitcode: null, unitname: null, username: null, userpsd: null }
tableName.userObjTableName = 'login_user'

// 嫌疑人基本信息
sqlObj.xyrjbxxObj = {
  bmsah: null,
  tysah: null,
  xyrbh: null,
  sfsc: null,
  cbdw_bm: null,
  cbdw_mc: null,
  fqdwbm: null,
  fql: null,
  cjsj: '1',
  zhxgsj: null,
  sfzf: null,
  xm: null,
  cym: null,
  ch: null,
  xb_dm: null,
  xb_mc: null,
  zp: null,
  zasnl: null,
  csrq: null,
  csd_dm: null,
  csd_mc: null,
  zjlx_dm: null,
  zjlx_mc: null,
  zjhm: null,
  mz_dm: null,
  mz_mc: null,
  gj_dm: null,
  gj_mc: null,
  hjszd_dm: null,
  hjszd_mc: null,
  zsd_dm: null,
  zsd_mc: null,
  zsdxxdz: null,
  gzdwszd_dm: null,
  gzdwszd_mc: null,
  gzdw: null,
  sf_dm: null,
  sf_mc: null,
  sjyzk_dm: null,
  sjyzk_mc: null,
  zzmm_dm: null,
  zzmm_mc: null,
  rddb_dm: null,
  rddb_mc: null,
  zxwy_dm: null,
  zxwy_mc: null,
  zw: null,
  zj_dm: null,
  zj_mc: null,
  zy_dm: null,
  zy_mc: null,
  sfdrsz: null,
  sfdwld: null,
  sfncjczzry: null,
  wcnfzxyrdjhrqk_dm: null,
  wcnfzxyrdjhrqk_mc: null,
  fddlr: null,
  szxxmc: null,
  dqqzcsbh: null,
  sfzt: null,
  jyksrq: null,
  jydqrq: null,
  jyyjzt: null,
  jyts: null,
  nl: null,
  sfdwzfld: null,
  sflxsc: null,
  scph: null,
  id: null
}
tableName.xyrjbxxObjTableName = 'tyyw_gg_xyrjbxx'

// 电子卷宗目录
sqlObj.dzjzmlObj = {
  jzbh: null,
  mlbh: null,
  sfsc: null,
  cjsj: null,
  zhxgsj: null,
  fqdwbm: null,
  fql: null,
  dwbm: null,
  bmsah: null,
  fmlbh: null,
  mlxx: null,
  mlsxh: null,
  mlbm: null,
  mllx: null,
  sfsm: null,
  sslbbm: null,
  sslbmc: null,
  mlxsmc: null,
  sfzxdj: null,
  nodeid: null,
  nodeparentid: null
}
tableName.dzjzmlObjTableName = 'yx_dzjz_jzml'

// 电子卷宗文件
sqlObj.dzjzmlwjObj = {
  jzbh: null,
  mlbh: null,
  wjxh: null,
  sfsc: null,
  cjsj: null,
  zhxgsj: null,
  fqdwbm: null,
  fql: null,
  dwbm: null,
  bmsah: null,
  wjlj: null,
  wjmc: null,
  wjxsmc: null,
  wjhz: null,
  wjksy: null,
  wjjsy: null,
  wjbzxx: null,
  wjyzbz: null,
  wjsxh: null,
  wjzdy: null,
  sslbbm: null,
  sslbmc: null,
  remark: null,
  pdf: null,
  sfzxdj: null,
  nodeid: null,
  nodeparentid: null
}
tableName.dzjzmlwjObjTableName = 'yx_dzjz_jzmlwj'

// 电子卷宗图片流
sqlObj.dzjztplObj = { wjxh: null, id: null, img: null, text: null, order: null }
tableName.dzjztplObjTableName = 'yx_dzjz_jztpl'

// 文书卷宗目录
sqlObj.wsjzmlObj = {
  bmsah: null,
  tysah: null,
  sfsc: null,
  fqdwbm: null,
  fql: null,
  cjsj: null,
  zhxgsj: null,
  dwbm: null,
  jzmlh: null,
  jzmlmc: null,
  jzmlbm: null,
  jzfmlh: null,
  jzmllx: null,
  sflxsc: null,
  scph: null,
  sfzxdj: null,
  nodeid: null,
  nodeparentid: null
}
tableName.wsjzmlObjTableName = 'yx_ws_ajjzml'

// 文书卷宗文件
sqlObj.wsjzmlwjObj = {
  bmsah: null,
  tysah: null,
  sfsc: null,
  fqdwbm: null,
  fql: null,
  cjsj: null,
  zhxgsj: null,
  dwbm: null,
  jzmlh: null,
  jzwjh: null,
  wsbh: null,
  xgrq: null,
  wjmc: null,
  wjlx: null,
  wscflj: null,
  fwsbh: null,
  nzrgh: null,
  nzrxm: null,
  wjkzm: null,
  rjsj: null,
  rjrgh: null,
  rjrxm: null,
  wjsx: null,
  gxywbms: null,
  sfkygx: null,
  wsmbbh: null,
  yzbh: null,
  yzmc: null,
  yyzt: null,
  dyzt: null,
  yyhwjbh: null,
  yjdyfs: null,
  byzdyfs: null,
  sfjh: null,
  wh: null,
  sflxsc: null,
  scph: null,
  remark: null,
  pdf: null,
  sfzxdj: null,
  nodeid: null,
  nodeparentid: null
}
tableName.wsjzmlwjObjTableName = 'yx_ws_ajjzwj'

// 文书卷宗图片流
sqlObj.wsjztplObj = { wsbh: null, id: null, img: null, text: null, order: null }
tableName.wsjztplObjTableName = 'yx_ws_ajjztpl'

for (let objName in sqlObj) {
  getSql[`get_insert_${objName.replace(/Obj/, '')}_sql`] = function (obj) {
    let objData = Object.assign({}, sqlObj[objName])
    for (let name in objData) {
      objData[name] = obj[name.toLowerCase()]
    }
    return getInsertSql(tableName[`${objName}TableName`], objData)
  }
}
for (let objName in sqlObj) {
  getSql[`get_insert_${objName.replace(/Obj/, '')}_variable_sql`] = function (obj) {
    let objData = Object.assign({}, sqlObj[objName])
    for (let name in objData) {
      objData[name] = obj[name.toLowerCase()]
    }
    return getInsertVariableSql(tableName[`${objName}TableName`], objData)
  }
}

/**
 * 获取插入sql
 * @param tableName 表名
 * @param obj 数据对象
 * @returns {string}
 */
export function getInsertSql (tableName, obj) {
  if (!isNotEmpty(obj) || !isNotEmpty(tableName)) throw new Error('两个参数不能为空')
  let fields = ''
  let values = ''
  for (let name in obj) {
    let value = obj[name]
    if (!value && value !== 0) continue
    value = typeof value.replace === 'function' ? value.replace(/'/g, `''`) : value
    fields += `,[${name}]`
    values += `,'${value}'`
  }
  if (!fields) return ''
  fields = fields.substr(1)
  values = values.substr(1)
  return `insert into ${tableName}(${fields}) values(${values});`
}

export function getInsertVariableSql (tableName, obj) {
  if (!isNotEmpty(obj) || !isNotEmpty(tableName)) throw new Error('两个参数不能为空')

  let param = JSON.parse(JSON.stringify(obj))

  let fields = ''
  let values = ''
  for (let name in param) {
    let value = param[name] || param[name] === 0 ? (param[name] + '') : ''

    if (value) {
      console.log('value', value)
      fields += `,[${name}]`
      values += `,$${name}`

      param['$' + name] = param[name]
    }
    delete param[name]
  }
  if (!fields) return ''
  fields = fields.substr(1)
  values = values.substr(1)

  return {
    SQL: `insert into ${tableName}(${fields}) values(${values});`,
    Parameter: param
  }
}

// 获取表信息
// PRAGMA table_info([login_user])
